/***************************************************************************
 *   Copyright (C) 2007 by Sebastian Bilski, Lance Cooper, Jeff Rebacz,
 *   									and Alex Zelenskiy
 *   sbilski@iit.edu
 *   lance.m.cooper@gmail.com
 *   jeff.rebacz@gmail.com
 *   voiceofanarchy@gmail.com
 *
 *	This file is part of connect3
 *
 *   connect3 is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License, version 2,
 *    as published by the Free Software Foundation
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc., 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 ***************************************************************************/

import java.awt.Color;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import junit.framework.TestCase;



public class Test_Protocol extends TestCase {
	
	Protocol p1;
	Protocol p2;
	GameBoard gb1;
	GameBoard gb2;
	GameLogic gl1;
	GameLogic gl2;
	ServerThread st;
	
	Player ply1;
	Player ply2;
	
	ServerSocket ss2;
	Socket s1;
	Socket s2;
	
	public Test_Protocol(String name){
		super(name);
	}	
	
	protected void setUp(){
		
		gb1 = new GameBoard(3,3);
		gb2 = new GameBoard(3,3);
		
		gl1 = new GameLogic();
		gl2 = new GameLogic();
		
		ply1 = new Player("ply1", Color.BLACK);
		ply2 = new Player("ply2", Color.BLUE);
		
		gl1.addPlayer(ply1);
		gl1.addPlayer(ply2);
		gl2.addPlayer(ply1);
		gl2.addPlayer(ply2);
		
		try {
			ss2 = new ServerSocket(6699);
			st = new ServerThread();
			st.start();
			s1 = new Socket("127.0.0.1", 6699);	
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("connection failed");
		}
		
		
		
	}
	
	protected void tearDown(){
		try {
			st.kill();
			s1.close();
			s2.close();
			ss2.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void testProtocols(){
		try {
			while(!s2.isConnected()){
				System.err.println(s2);
			}
			while(!s1.isConnected()){
				System.err.println(s2);
			}
			p2 = new Protocol(s2, gl2, gb2);
			p1 = new Protocol(s1, gl1, gb1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		p1.sendChat("hello");
		p2.readProtoMessage();
		assertTrue(p2.getChat().equals("hello\n"));
				
		p1.sendWon(3);
		p2.readProtoMessage();
		assertTrue(p2.getWinner()==3);
		
		p1.sendDisconnect(2);
		p2.readProtoMessage();
		assertTrue(p2.getDisconnect()==2);
		
	}
	
	
//	thread that runs the accept loop
	public class ServerThread extends Thread {
		
		boolean x=true;
		public void run()
		{
			
			//server accept loop
			while(x)
			{				
				try {
					s2 = ss2.accept();
				} catch (IOException e) {
					System.err.println("Failed to accept client connection, terminating");
					return;
				}
			}
			try {
				s2.close();
				ss2.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public void kill(){
			x=false;
		}
	}
	
}
